package xyz.hzxiao.config.mybatis;


import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.hzxiao.base.AbstractEntity;

import java.util.Collections;
import java.util.Scanner;

/**
 * @Author: scy
 * @Date: 2022-07-29 10:30
 */
public class FuXxOfGenerator {

    private static final Logger LOGGER = LoggerFactory.getLogger(FuXxOfGenerator.class);
    private static final String AUTHOR = "FuXx";
    private static final String URL = "jdbc:mysql://192.168.70.127:3306/demo?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true";
    private static final String USER_NAME = "root";
    private static final String PASSWORD = "Ymkj666666@";
    private static final String PROJECT_ROOT_DIR = System.getProperty("user.dir") + "/demo-user/src/main/java";

    public static void main(String[] args) {
        String modelName = scanner("模块名");
        String[] tableNameArray = scanner("表名，多个英文逗号分割").split(",");
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator(new DataSourceConfig.Builder(URL, USER_NAME, PASSWORD).build())
                .global(new GlobalConfig.Builder()
                        .outputDir(PROJECT_ROOT_DIR)
                        .author(AUTHOR)
                        .commentDate("yyyy-MM-dd")
                        .disableOpenDir()
                        .build())
                .packageInfo(new PackageConfig.Builder()
                        .parent("xyz.hzxiao")
                        .moduleName(modelName)
                        .entity("entity")
                        .service("service")
                        .serviceImpl("service.impl")
                        .mapper("mapper")
                        .xml("mapper")
                        .controller("controller")
                        .pathInfo(Collections.singletonMap(OutputFile.entity, System.getProperty("user.dir") + "\\demo-common\\src\\main\\java\\xyz\\hzxiao\\entity\\" + modelName))
                        .build())
                .template(new TemplateConfig.Builder()
                        .disable(TemplateType.ENTITY)
                        .entity("/templates/entity.java")
                        .service("/templates/service.java")
                        .serviceImpl("/templates/serviceImpl.java")
                        .mapper("/templates/mapper.java")
                        .xml("/templates/mapper.xml")
                        .controller("/templates/controller.java")
                        .build())
                .strategy(new StrategyConfig.Builder()
                        .addInclude(tableNameArray)
                        .addTablePrefix(modelName.concat("_"))
                        .entityBuilder()
                        .superClass(AbstractEntity.class)
                        .enableChainModel()
                        .enableLombok()
                        .enableRemoveIsPrefix()
                        .enableActiveRecord()
                        .enableColumnConstant()
                        .naming(NamingStrategy.underline_to_camel)
                        .columnNaming(NamingStrategy.underline_to_camel)
                        .addSuperEntityColumns("id", "created_time", "updated_time")
                        .addTableFills(new Column("create_time", FieldFill.INSERT))
                        .addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE))
                        .idType(IdType.AUTO)
                        .formatFileName("%sEntity")
                        .build()
                        .controllerBuilder()
                        .enableHyphenStyle()
                        .enableRestStyle()
                        .formatFileName("%sController")
                        .build()
                        .serviceBuilder()
                        .superServiceClass(IService.class)
                        .superServiceImplClass(ServiceImpl.class)
                        .formatServiceFileName("%sService")
                        .formatServiceImplFileName("%sServiceImpl")
                        .build()
                        .mapperBuilder()
                        .superClass(BaseMapper.class)
                        .enableMapperAnnotation()
                        .formatMapperFileName("%sMapper")
                        .build());
        mpg.execute();
    }

    private static String scanner(String tip) {
        Scanner input = new Scanner(System.in);
        LOGGER.info("输入{}:", tip);
        if (input.hasNext()) {
            return input.next();
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }
}
